package com.woniuxy.yoga.commons.web.util;


import com.woniuxy.yoga.commons.oss.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@Component
public class UpLoadFileUtil {

    @Autowired
    private OSSUtil ossUtil;

    public String upLoadFile(MultipartFile file) throws Exception {
        if (file == null) throw new Exception("文件为空");
        String filename = file.getOriginalFilename();
        if (filename == null) throw new Exception("文件为空");
        String imageName = UUID.randomUUID().toString().replaceAll("-", "") + filename.substring(filename.lastIndexOf("."));
        return ossUtil.uploadFile(file.getInputStream(), imageName, ossUtil.getBucketName());
    }

    public String upLoadFile(InputStream inputStream,String type) throws Exception {
        if (inputStream == null) throw new Exception("文件为空");
        String imageName = UUID.randomUUID().toString().replaceAll("-", "") + "." + type;
        return ossUtil.uploadFile(inputStream, imageName, ossUtil.getBucketName());
    }

    public boolean removeFile(String imagePath) throws Exception {
        if (imagePath == null || "".equals(imagePath)) throw new Exception("文件地址为空");
        return ossUtil.deleteFile(imagePath, ossUtil.getBucketName());
    }
}
